import 'package:extended_image/extended_image.dart';
import 'package:flutter_qiniu/flutter_qiniu.dart';
import 'package:gd_assistant/model/doctorModel.dart';
import 'package:gd_assistant/api/apiHandler.dart';
import 'package:gd_assistant/util/dataUtil.dart';
import 'package:lpinyin/lpinyin.dart';
import 'package:azlistview/azlistview.dart';

class publicUtil {
  //七牛上传图片批量
  static Future<List<String>> onUploadFiles(List<String> entities) async {
    final qiniu =  FlutterQiniu(zone: QNFixedZone.zone0);
    List<FilePathEntity> paths = [];
    int i = 0;
    for (String path in entities) {
      i ++;
      paths.add(FilePathEntity(filePath: path, key: publicUtil.getRandomText(vkey: i.toString()),token: dataUtil.getValue(dataUtil.upLoadTokenKey)));
    }
    print(paths);
    List<String> resultKeys = await qiniu.uploadFiles(paths);
    return resultKeys;
  }

  //获取一个不会重复的随机文字串
  //规则是 用户 tokenID + wangpai + 时间戳
  static getRandomText({String vkey = ""}) {
    String key = keyToMd5(dataUtil.getValue(dataUtil.assistantIdKey)+"wangpai"+DateTime.now().millisecondsSinceEpoch.toString()+ vkey);
    print(key);
    return key;
  }
   
  //获取有服务包的医生数据
  static getServiceDoctor(List<DoctorModel> list) async {
    Map doctorMap = await apiHandler.getServicePackageDoctorMap();
      doctorMap.forEach((v,k){
        k.forEach((value) {
         list.add(DoctorModel(name: value['name'],id: value["_id"],avatar: value["avatar"],hospital: value["hospital"], isSelected: false));
       });
      });
      _handleList(list);
  }

  static _handleList(List<DoctorModel> list) {
    if (list == null || list.isEmpty) return;
    for (int i = 0, length = list.length; i < length; i++) {
      String pinyin = PinyinHelper.getPinyinE(list[i].name);
      String tag = pinyin.substring(0, 1).toUpperCase();
      list[i].namePinyin = pinyin;
      if (RegExp("[A-Z]").hasMatch(tag)) {
        list[i].tagIndex = tag;
      } else {
        list[i].tagIndex = "#";
      }
    }
    //根据A-Z排序
    SuspensionUtil.sortListBySuspensionTag(list);
  }
}